package distributeCandies;

import java.util.Arrays;

public class Solution {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(distributeCandies(120,4)));
    }
    public static int[] distributeCandies(int candies, int num_people) {
        int[]arr=new int[num_people];

        //先计算出k
        int k=(int)(-1+Math.sqrt((1+8*(long)candies)))/(2*num_people);
        System.out.println(k);
        for (int i = 0; i < num_people; i++) {
            arr[i]=(k-1)*k/2*num_people+k*(i+1);
        }
        System.out.println(Arrays.toString(arr));
        long rest=candies-(num_people*k)*(num_people*k+1)/2;
        System.out.println(rest);
        int o=num_people*(k);
        for (int i = 0;rest>0; i++) {
            int t=o+i+1;
            int add= rest-t>0?t:(int) rest;
            rest-=add;
            arr[i]+=add;
        }

        return arr;
    }
    // 4学生
    // 1  2  3  4
    // 5  6  7  8
    // 9 10 11 12
    //15 18 21 24       0+1+2+3+..k-1  (k-1)*k/2       第i个学生  (n*k)*(n*k+1)/2 == candies i*(i+1)=2*can
    //
    // 1 2 3
    // 4 5 6
    //
}
